package com.wsoft.bpm.enums;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 执行类型
 *
 * @Author zhonglj
 * @Date 2024-09-19 15:38
 */
public enum ExecuteType {
    /**
     * 指定人
     */
    ASSIGNEE("assignee", null, "指定人"),
    /**
     * 候选人
     */
    CANDIDATE_USER("candidateUser", null, "候选人"),
    /**
     * 候选组织
     */
    CANDIDATE_ORG("candidateOrg", "org", "候选组织"),

    /**
     * 候选角色
     */
    CANDIDATE_ROLE("candidateRole", "role", "候选角色"),

    /**
     * 候选职务级别
     */
    CANDIDATE_LEVEL("candidateLevel", "level", "候选职务级别"),

    /**
     * 候选岗位
     */
    CANDIDATE_POSITION("candidatePosition", "position", "候选岗位"),
    /**
     * 执行表达式
     */
    EXECUTE_EXPRESSION("expression", null, "执行表达式");

    private String value;
    private String activitiCode;
    private String description;

    ExecuteType(String value, String activitiCode, String description) {
        this.value = value;
        this.activitiCode = activitiCode;
        this.description = description;
    }

    public String getValue() {
        return value;
    }

    public static String getCandidateGroups(String type, String expression) {
        if (StrUtil.isBlank(type)) {
            return expression;
        }

        ExecuteType find = null;
        for (ExecuteType item : values()) {
            if (item.getValue().equals(type)) {
                find = item;
                break;
            }
        }

        if (find == null) {
            return expression;
        }
        String activitiCode = find.activitiCode;
        List<String> datas = StrUtil.split(expression, ",");
        if (CollUtil.isNotEmpty(datas)) {
            return datas.stream().map(e -> activitiCode + ":" + e).collect(Collectors.joining(","));
        }

        return expression;
    }
}
